package org.imlinker.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.imlinker.login.Account;
import org.imlinker.login.Login;
import org.imlinker.util.StringUtils;

/**
 * 登录窗口.
 */

@SuppressWarnings("serial")
public class LoginWindow extends LocationSavedWindow {

	public LoginWindow(final Login paraLogin) {
		super();

		this.login = paraLogin;

		// 初始化GUI组件
		initComponents();

		initActions();

		// 固定设置为屏幕居中。
		setLocationRelativeTo(null);
		setVisible(true);
	}

	public void initActions() {
		passwordField.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				login();
			}

		});

		passwordField.addFocusListener(new FocusAdapter() {

			public void focusGained(FocusEvent e) {
				passwordField.selectAll();
			}

		});

		loginButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				login();
			}

		});

		checkSavePass.addItemListener(new ItemListener() {

			public void itemStateChanged(ItemEvent e) {
				if (!checkSavePass.isEnabled()) {
					return;
				}

				if (e.getStateChange() == ItemEvent.SELECTED) {
					int result = JOptionPane
							.showConfirmDialog(
									LoginWindow.this,
									"It is not safe to save pasword in local file.Save?",
									"SavePassword",
									JOptionPane.OK_CANCEL_OPTION);
					if (result == JOptionPane.CANCEL_OPTION) {
						checkSavePass.setSelected(false);
					}
				}
			}

		});
	}

	/**
	 * 登录失败，更新状态栏，恢复失效的组件。
	 */
	public void loginFailed(final String info) {
		setStatus(info);

	}

	/**
	 * 更新窗口底部的状态栏.
	 * 
	 * @param status
	 *            新的状态
	 */
	public void setStatus(final String status) {
		statusInfoLabel.setText(status);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		javax.swing.JTabbedPane jTabbedPane1 = new javax.swing.JTabbedPane();
		javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
		userField = new javax.swing.JTextField();
		javax.swing.JLabel usernameLabel = new javax.swing.JLabel();
		javax.swing.JLabel passwordLabel = new javax.swing.JLabel();
		passwordField = new javax.swing.JPasswordField();
		loginButton = new javax.swing.JButton();
		checkSavePass = new javax.swing.JCheckBox();
		javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();
		javax.swing.JLabel statusLabel = new javax.swing.JLabel();
		statusInfoLabel = new javax.swing.JLabel();

		bundle = java.util.ResourceBundle.getBundle("org/imlinker/gui/Bundle"); // NOI18N

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle(bundle.getString("LoginWindow.title")); // NOI18N
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setName("LoginWindow");

		usernameLabel.setText(bundle
				.getString("LoginWindow.usernameLabel.text")); // NOI18N
		usernameLabel.setName("jLabel1"); // NOI18N

		passwordLabel.setText(bundle
				.getString("LoginWindow.passwordLabel.text")); // NOI18N
		passwordLabel.setName("jLabel2"); // NOI18N

		passwordField.setName("jPasswordField1"); // NOI18N

		loginButton.setText(bundle.getString("LoginWindow.loginButton.text")); // NOI18N
		loginButton.setName("jButton1"); // NOI18N

		checkSavePass
				.setText(bundle.getString("LoginWindow.savePassword.text")); // NOI18N
		checkSavePass.setName("jCheckBox1"); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGap(
																				19,
																				19,
																				19)
																		.addComponent(
																				passwordLabel))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				usernameLabel)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				loginButton)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				checkSavePass))
														.addComponent(
																passwordField,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																321,
																Short.MAX_VALUE)
														.addComponent(
																userField, 0,
																321,
																Short.MAX_VALUE))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																usernameLabel)
														.addComponent(
																userField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																passwordField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																passwordLabel))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																loginButton)
														.addComponent(
																checkSavePass))
										.addGap(35, 35, 35)));

		jTabbedPane1.addTab(bundle
				.getString("LoginWindow.basicPanel.TabConstraints.tabTitle"),
				jPanel1); // NOI18N
		jSeparator1.setName("jSeparator1"); // NOI18N

		statusLabel.setText(bundle.getString("LoginWindow.statusLabel.text")); // NOI18N
		statusLabel.setName("jLabel4"); // NOI18N

		statusInfoLabel
				.setText(bundle.getString("LoginWindow.statusInfo.text")); // NOI18N
		statusInfoLabel.setName("jLabel5"); // NOI18N

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
						404, Short.MAX_VALUE).addContainerGap()).addComponent(
				jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 428,
				Short.MAX_VALUE).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						statusLabel).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(statusInfoLabel,
								javax.swing.GroupLayout.DEFAULT_SIZE, 359,
								Short.MAX_VALUE).addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jTabbedPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												166,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																statusLabel)
														.addComponent(
																statusInfoLabel))
										.addContainerGap()));

		pack();
	} // </editor-fold>

	/**
	 * 点击登录按钮或者在密码输入框按回车，触发的登录事件.
	 */
	private void login() {
		// 获得用户名和密码，同时判断在为空的情况下提出警告
		String username = userField.getText();
		String password = new String(passwordField.getPassword());
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			return;
		}

		// 把登录信息包装成一个Account。
		Account account = new Account();

		account.setUser(username);
		account.setPassword(password);

		// 把Account和是否保存密码的信息提交给login线程。
		login.setAccount(account);
	}

	// Variables declaration - do not modify
	private javax.swing.JButton loginButton;

	private javax.swing.JCheckBox checkSavePass;

	private javax.swing.JTextField userField;

	private javax.swing.JLabel statusInfoLabel;

	private javax.swing.JPasswordField passwordField;

	private Login login;

	private ResourceBundle bundle;

	// End of variables declaration

}
